package com.naihe.anderson.sms.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.util.LockPatternUtils;
import com.naihe.anderson.sms.widgets.LockPatternView;
import com.naihe.anderson.sms.widgets.LockPatternView.Cell;

public class SetPasswordActivity extends Activity implements OnClickListener {

	public NhSmsApp nhSmsApp;
	public NhSmsMgr nhSmsMgr;
	private String passwordStep0;

	private String passwordCurrent;

	private Button btnSpLeft, btnSpRight;
	private TextView TopText;
	private LockPatternView mLockPatternView;

	private int step = 0;

	private static final int MSG_WHAT_CLEARPATTERN = 0;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_CLEARPATTERN: {
				btnSpLeft.setText(R.string.cancel);
				btnSpLeft.setTag("0000");
				if(step == 1){
					TopText.setText(R.string.pattern_again);
				}
				mLockPatternView.clearPattern();
			}
			default:
				break;
			}
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setpassword);

		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		nhSmsMgr = nhSmsApp.getNhSmsMgr();

		btnSpLeft = (Button) findViewById(R.id.btnSpLeft);
		btnSpRight = (Button) findViewById(R.id.btnSpRight);
		TopText = (TextView) findViewById(R.id.SpTopText);
		mLockPatternView = (LockPatternView) findViewById(R.id.SpLockPattern);

		if (TextUtils.isEmpty(nhSmsMgr.getBaseSafetyCipher())) {
			TopText.setText(R.string.pattern_to_draw);
			btnSpRight.setText(R.string.pattern_to_next);
			btnSpRight.setBackgroundResource(R.drawable.alert_system_press);
			btnSpLeft.setText(R.string.cancel);
			btnSpLeft.setTag("0000");
		}
		btnSpLeft.setOnClickListener(this);

		mLockPatternView.setOnPatternListener(new LockPatternView.OnPatternListener() {
			public void onPatternStart() {
				TopText.setText(R.string.pattern_start);
			}

			public void onPatternCleared() {

			}

			public void onPatternCellAdded(List<Cell> pattern) {
			}

			public void onPatternDetected(List<Cell> pattern) {
				btnSpLeft.setTag("1111");
				btnSpLeft.setText(R.string.pattern_to_reset);
				
				if (pattern.size() >= 4) {
					mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Correct);
					passwordCurrent = LockPatternUtils.patternToString(pattern);
					if(step == 0){
						TopText.setText(R.string.pattern_end);
						btnSpRight.setBackgroundResource(R.drawable.alert_system_btn);
						btnSpRight.setOnClickListener(SetPasswordActivity.this);
					} else if (step == 1) {
						if (!passwordCurrent.equals(passwordStep0)) {
							TopText.setText(R.string.pattern_password_wrong);
							mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
							mHandler.sendEmptyMessageDelayed(MSG_WHAT_CLEARPATTERN, 1000);
						} else {
							TopText.setText(R.string.pattern_end);
							btnSpRight.setBackgroundResource(R.drawable.alert_system_btn);
							btnSpRight.setOnClickListener(SetPasswordActivity.this);
						}
					}
				} else {
					TopText.setText(R.string.patternlimit);
					mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
					mHandler.sendEmptyMessageDelayed(MSG_WHAT_CLEARPATTERN, 1000);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnSpRight) {
			if (step == 0) {
				TopText.setText(R.string.pattern_again);
				btnSpLeft.setText(R.string.cancel);
				btnSpLeft.setTag("0000");
				mHandler.sendEmptyMessage(MSG_WHAT_CLEARPATTERN);
				btnSpRight.setText(R.string.ok);
				btnSpRight.setBackgroundResource(R.drawable.alert_system_press);
				btnSpRight.setOnClickListener(null);
				passwordStep0 = passwordCurrent;
				step = 1;
			} else if (step == 1) {
				nhSmsMgr.setBaseSafetyCipher(passwordCurrent);
//				Toast.makeText(SetPasswordActivity.this, "密码已经设置", Toast.LENGTH_SHORT).show();
				nhSmsApp.setActivity(null);
				SetPasswordActivity.this.finish();
			}
		} else if (v.getId() == R.id.btnSpLeft) {
			if (v.getTag().equals("0000")) {
				Intent intent = new Intent(SetPasswordActivity.this, BaseSetActivity.class);
				SetPasswordActivity.this.setResult(RESULT_OK, intent);
				nhSmsApp.setActivity(null);
				SetPasswordActivity.this.finish();
			} else if (v.getTag().equals("1111")) {
				mHandler.sendEmptyMessage(MSG_WHAT_CLEARPATTERN);
				TopText.setText(R.string.pattern_to_draw);
				btnSpRight.setText(R.string.pattern_to_next);
				btnSpRight.setBackgroundResource(R.drawable.alert_system_press);
				btnSpRight.setOnClickListener(null);
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(SetPasswordActivity.this, BaseSetActivity.class);
			SetPasswordActivity.this.setResult(RESULT_OK, intent);
			nhSmsApp.setActivity(null);
			SetPasswordActivity.this.finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
}
